package cn.innosmart.aq.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.ArrayList;

import cn.innosmart.aq.R;
import cn.innosmart.aq.bean.AlarmConditionBean;
import cn.innosmart.aq.bean.AquariumBean;
import cn.innosmart.aq.bean.DeviceBean;
import cn.innosmart.aq.util.DeviceConstant;
import cn.innosmart.aq.util.SystemConstant;

/**
 *@Deprecated by David Replace by GridView
 * Created by David on 2015/7/28.
 */
public class PhListAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private Context context;
    private ArrayList<String> items;

    public PhListAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        items = new ArrayList<String>();
        this.context = context;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listview_ph, null);
            holder.tv_ph_value = (TextView) convertView.findViewById(R.id.tv_ph_value);
            holder.tv_alert_value = (TextView) convertView.findViewById(R.id.tv_alert_value);
            holder.ivEditPh = (ImageView) convertView.findViewById(R.id.iv_edit_ph);
            holder.llBackground = (LinearLayout) convertView.findViewById(R.id.ll_background);
            holder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.ivAlarm = (ImageView) convertView.findViewById(R.id.iv_alarm);
            holder.rlEdit = (RelativeLayout) convertView.findViewById(R.id.rl_edit_ph);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        DeviceBean deviceBean = null;
        if (items.size() == 0) {
            holder.tv_ph_value.setText(context.getString(R.string.listview_temperature_unknown_value));
            holder.tv_alert_value.setText(context.getString(R.string.listview_value_unset));
        } else {
            deviceBean = SystemConstant.deviceBeanHashMap.get(items.get(position));
            double value = deviceBean.getSensorvalue();
            int sensorunity = deviceBean.getSensorunit();
            if (value == 0.0) {
                holder.tv_ph_value.setText("加载中");
            } else {
                holder.tv_ph_value.setText(DeviceConstant.caculateValue(sensorunity, value));
            }
            StringBuilder sb = new StringBuilder();
            if (DeviceConstant.alarmConditionBeanHashMap != null) {
                AlarmConditionBean alarmConditionBean = DeviceConstant.alarmConditionBeanHashMap.get(deviceBean.getAddress());
                if (alarmConditionBean != null) {
                    sb.append(alarmConditionBean.getAboveValue() + "<值<" + alarmConditionBean.getBelowValue());
                }
            }
            holder.tv_alert_value.setText(sb.length() == 0 ? context.getString(R.string.listview_value_unset) : sb.toString());
        }
        if (deviceBean.isAlarm()) {
            holder.ivAlarm.setVisibility(View.VISIBLE);
        } else {
            holder.ivAlarm.setVisibility(View.GONE);
        }
        if (deviceBean != null) {
            holder.ivIcon.setTag(deviceBean);
            holder.ivIcon.setOnClickListener(onClickListener);
            holder.rlEdit.setTag(deviceBean);
            holder.rlEdit.setOnClickListener(onClickListener);
        }


        return convertView;
    }

    static class ViewHolder {
        TextView tv_ph_value;
        TextView tv_alert_value;
        ImageView ivEditPh;
        LinearLayout llBackground;
        ImageView ivAlarm;
        ImageView ivIcon;
        RelativeLayout rlEdit;
    }

    private View.OnClickListener onClickListener;

    public void setClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    private AquariumBean aquariumBean;

    public void refreshData(AquariumBean aquariumBean) {
        this.aquariumBean = aquariumBean;
        items = aquariumBean.getPhs();
        System.out.println("PhListAdapter=" + items.size());
        notifyDataSetChanged();
    }
}
